#include<bits/stdc++.h>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode():val(0),left(nullptr),right(nullptr){}
    TreeNode(int x):val(x),left(nullptr),right(nullptr){}
    TreeNode(int x,TreeNode* left,TreeNode* right):val(x),left(left),right(right){}
    /* data */
};


class Solution
{
private:
    /* data */

public:
    int sumOfLeftLeaves(TreeNode* root){
        if(root==nullptr){
            return 0;
        }
        if(root->left==nullptr &&  root->right==nullptr){
            return 0;
        }
        int res=0;
        queue<TreeNode*> que;
        que.emplace(root);
        while(!que.empty()){
            int len=que.size();
            while(len--){
                TreeNode* temp=que.front();
                // cout<<temp->val<<endl;
                que.pop();
                if(temp->left!=nullptr && temp->left->left==nullptr && temp->left->right==nullptr){
                    res+=temp->left->val;
                    // cout<<res<<endl;
                }
                if(temp->left!=nullptr){
                    que.emplace(temp->left);
                }
                if(temp->right!=nullptr){
                    que.emplace(temp->right);
                }
            }
        }
        return res;
    }

};



int main(int argc, char const *argv[])
{
    TreeNode* node1=new TreeNode(3);
    TreeNode* node2=new TreeNode(9);
    TreeNode* node3=new TreeNode(20);
    TreeNode* node4=new TreeNode(15);
    TreeNode* node5=new TreeNode(7);
    node1->left=node2;
    node1->right=node3;
    node3->left=node4;
    node3->right=node5;
    // cout<<node1->left->val<<endl;
    Solution s;
    int res = s.sumOfLeftLeaves(node1);
    cout<<res<<endl;
    return 0;
}
